Embedded programming

This week focused on programming the PCB created in week 4. While it may not involve as much physical work, it requires thoughtful analysis and programming skills. The challenge lies in conceptualizing and implementing the desired functionality, making it as simple or complex as desired. This aspect opens the mind and the creativity of the programmer.

First program

Arduino

Arduino is a versatile platform equipped with a text editor for code writing, a message area, a text console, and a toolbar featuring commonly used functions, complemented by various menus. It facilitates program uploading and communication with Arduino hardware. In this scenario, we'll utilize it with the Seeed Xiao RP2040.

To utilize this software, the initial step is to install it and incorporate the Seeed Xiao RP2040 board. I've elaborated on this process in week 3, which you can review by clicking here. This ensures communication for uploading sketches to the board.

With the software downloaded, open a new sketch and begin coding. The initial software was quite simple, it involved utilizing the button on the PCB to regulate the brightness of the three LEDs also present on the PCB. This was achieved using PWM, so as long as the button is pressed, the brightness of the LEDs would change.


The important aspect of this code is how the PWM is declared. In Arduino, PWM is more analog-like, although it essentially involves switching on and off the voltage. The analogWrite instruction in Arduino can handle a maximum value of 255, counting from 0, which limits it to a resolution of 8 bits. If you require higher resolution, you need to adjust how you set up the analog output.

MU editor

To use CircuitPython with the MU text editor, you first need to install MU by visiting the MU website and downloading it from there or by clicking here. Then, to install CircuitPython on your board, follow these steps:

  • Go to the group assignments page, which I'll provide the link at the end.
  • Scroll to the bottom of the group assignments page and find the CircuitPython section.
  • Click on the CircuitPython highlight to download the file.
  • With your board turned off, hold down the B button on the board and connect it to your computer.
  • A file explorer window will open. Drop the downloaded CircuitPython file into this window.
  • After completing these steps, you should have CircuitPython installed on your board and ready to use with the MU text editor.

From here, write this code, wich it functions the same as the arduino


In this case, with CircuitPython, you'll need to import the pwmio library to use PWM, and then specify which components will utilize PWM. After that, initialize the button for input. From there, the process is similar. With CircuitPython, you can achieve a resolution of 16 bits, providing more precise control over PWM outputs compared to Arduino's 8-bit resolution.

Second program

With my second program, I aimed to develop a entertaining game. I expanded the functionality by integrating an additional button and an external buzzer alongside the PCB. Initially, it posed some challenges, but as I delved into writing the code, it gradually became more manageable. I opted to use Arduino for this project because I find it more intuitive and easier to grasp the underlying concepts.


As you can see, the code is quite lengthy, but a significant portion of it is dedicated to playing the song. Specifically, I integrated the Pac-Man theme song into the program. I obtained this code from a GitHub repository, which you can access by clicking here. What I appreciate about this GitHub repository is that it offers a variety of songs that can be played using the buzzer.

Certainly, interruptions are a crucial tool in programming, especially for breaking the sequence of execution when certain conditions are met. In my case, interruptions helped me execute the game smoothly when it was triggered by external events. It's important to note that when using interruptions, the associated function needs to be declared as a void and should not take any arguments. Additionally, it's essential to be aware that certain functions and tools may not work as expected when executed within an interruption, so it's necessary to carefully manage concurrent tasks if interruptions are being used.

Group assignment

This week's group assignment focused on familiarizing ourselves with the datasheet for the microcontroller we use. Additionally, we compared the performance of our microcontroller with other architectures to ascertain its superiority. You can consult this valuable resource by clicking here.

Useful links